package com.example.chatzsyproject.ui;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.chatzsyproject.R;
import com.example.chatzsyproject.adapter.ChooseFriendAdapter;
import com.example.chatzsyproject.base.BaseActivity;
import com.example.chatzsyproject.model.FriendModel;
import com.example.chatzsyproject.utils.PinyinUtils;
import com.example.chatzsyproject.view.AZListView.azlist.AZItemEntity;
import com.example.chatzsyproject.view.AZListView.azlist.AZTitleDecoration;
import com.example.chatzsyproject.view.AZListView.azlist.AZWaveSideBarView;
import com.example.chatzsyproject.view.AZListView.azlist.LettersComparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 选择好友
 */
public class ChooseFriendActivity extends BaseActivity {
    ChooseFriendAdapter mAdapter;
    @BindView(R.id.bar_list)
    AZWaveSideBarView mBarList;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    List<AZItemEntity<FriendModel>> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choosefriend);
        ButterKnife.bind(this);
        init();
    }

    public void init() {
        recyclerView.setLayoutManager(new LinearLayoutManager(ChooseFriendActivity.this));
        recyclerView.addItemDecoration(new AZTitleDecoration(new AZTitleDecoration.TitleAttributes(ChooseFriendActivity.this)));
        mAdapter = new ChooseFriendAdapter(list);
        recyclerView.setAdapter(mAdapter);
        mBarList.setOnLetterChangeListener(new AZWaveSideBarView.OnLetterChangeListener() {
            @Override
            public void onLetterChange(String letter) {
                int position = mAdapter.getSortLettersFirstPosition(letter);
                if (position != -1) {
                    if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
                        LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                        manager.scrollToPositionWithOffset(position, 0);
                    } else {
                        recyclerView.getLayoutManager().scrollToPosition(position);
                    }
                }
            }
        });
        getData();
    }

    public void getData() {
        for (int i = 0; i < 5; i++) {
            List<FriendModel> itemFriend = new ArrayList<>();
            FriendModel friendModel = new FriendModel();
            friendModel.setId(1);
            friendModel.setTag("item");
            friendModel.setHead_image("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1571215845389&di=0650e873d33f1ae914016bd41d1c3201&imgtype=0&src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201501%2F08%2F20150108164231_t432j.thumb.700_0.png");
            friendModel.setUser_name("官御天");
            itemFriend.add(friendModel);
            FriendModel friendMode2 = new FriendModel();
            friendMode2.setId(2);
            friendMode2.setTag("item");
            friendMode2.setHead_image("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1571215845389&di=91bb0746a157533e7031628636eedd02&imgtype=0&src=http%3A%2F%2Fwww.banyingle.com%2Fshop%2Fimages_shop%2F24622%2F2012-01-12%2F69872012112106083970703.jpg");
            friendMode2.setUser_name("任千行");
            itemFriend.add(friendMode2);
            FriendModel friendMode3 = new FriendModel();
            friendMode3.setId(2);
            friendMode3.setTag("item");
            friendMode3.setHead_image("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1571215845389&di=5a8f2611f43013847b22eec0493dcb00&imgtype=0&src=http%3A%2F%2Fpic3.zhimg.com%2F50%2Fv2-dae702738ed0a8aa674e5d68d6eaf7e0_hd.jpg");
            friendMode3.setUser_name("哪吒");
            itemFriend.add(friendMode3);
            FriendModel friendMode4 = new FriendModel();
            friendMode4.setId(2);
            friendMode4.setHead_image("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1571215845388&di=62f8c7d6ff9530628ba4e6744e843e33&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F9e82e82fa2f69c1426df9fc2335e4b7439630b6b10d15-aqBAmh_fw658");
            friendMode4.setTag("item");
            friendMode4.setUser_name("燕藏锋");
            itemFriend.add(friendMode4);
            FriendModel friendMode5 = new FriendModel();
            friendMode5.setId(2);
            friendMode5.setTag("item");
            friendMode5.setHead_image("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1571215845387&di=9b3dd43ed6f5e72e5a7e281542729144&imgtype=0&src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201603%2F09%2F20160309105557_iRkhe.thumb.700_0.jpeg");
            friendMode5.setUser_name("##");
            itemFriend.add(friendMode5);

            list.addAll(fillData(itemFriend));
        }

        Collections.sort(list, new LettersComparator());
        mAdapter.notifyDataSetChanged();
    }

    private List<AZItemEntity<FriendModel>> fillData(List<FriendModel> itemFriendList) {
        List<AZItemEntity<FriendModel>> sortList = new ArrayList<>();
        for (FriendModel aDate : itemFriendList) {
            AZItemEntity<FriendModel> item = new AZItemEntity<>();
            item.setValue(aDate);
            //汉字转换成拼音
            String pinyin = PinyinUtils.getPingYin(aDate);
            //取第一个首字母
            String letters = pinyin.substring(0, 1).toUpperCase();
            // 正则表达式，判断首字母是否是英文字母
            if (letters.matches("[A-Z]")) {
                item.setSortLetters(letters.toUpperCase());
            } else {
                item.setSortLetters("#");
            }
            sortList.add(item);
        }
        return sortList;
    }

}
